<?php 

class ocorrencia_menuModel extends Model{
    
    private $menu_array = array(
        'always' => array(
            'Inicial'            => "ocorrencia/index/",
            'Mensagens'          => "ocorrencia/ocorrencias/unread/",
            'Grupos de trabalho' => "ocorrencia/grupo/",
            'Usuários'           => "ocorrencia/usuarios/",
            'Minha Conta' => array(
                'Minha Conta'   => 'usuario/login/editar/',
                'Meu Perfil'    => 'usuario/login/',
                'Configurações' => 'ocorrencia/configuracoes/',
                'Ajuda'         => 'ocorrencia/configuracoes/ajuda',
                'Sair'          => 'usuario/login/logout/',
             )
        ),
        
        'usuarios' => array(
            'Todos os usuários' => 'ocorrencia/usuarios/',
            'Novo Usuário'      => 'ocorrencia/usuarios/formulario/'
        )
    );
    
    private $needAdmin = array(
        'index' => array(
            'Adicionar Grupo' => "ocorrencia/grupo/formulario"
        )
    );

    public function getMenu($vars){
        
        $this->LoadResource('html', 'html');
        $this->LoadModel('usuario/login', 'uobj');
        
        $arr = array();
        if($this->uobj->UserIsAdmin()){
             if(array_key_exists(CURRENT_CONTROLLER, $this->needAdmin)){
                 $arr = $this->needAdmin [CURRENT_CONTROLLER];
                 foreach($arr as $name => $nd){
                     if($this->is_equal($nd)) unset($arr[$name]);
                 }
             }
        }
        
        if(CURRENT_URL == 'ocorrencia/grupo/formulario'){
            unset($this->menu_array['grupo']['Criar Discussão']);
        }
        
        if(array_key_exists(CURRENT_CONTROLLER, $this->menu_array)){
             $arr2 = $this->menu_array[CURRENT_CONTROLLER];
             foreach($arr2 as $name => $nd){
                 if($this->is_equal($nd)) unset($arr2[$name]);
             }
             $arr = array_merge_recursive($arr2, $arr);
        }
        
        $this->LoadJsPlugin('menu/multiple', 'men');
        $this->addMenu('body-top', $arr, $this->men);
        
        $this->menu_lateral();
    }
    
    private function is_equal($url){
         $temp = explode("/", $url);
         $total = count($temp);
         if($total > 3) $total = 3;
         $out = "";
         
         for($i = 0; $i < $total; $i++) {
             if($temp[$i] != "") $out .= $temp[$i]."/";
         }
         $rurl = CURRENT_MODULE . "/" .CURRENT_CONTROLLER . "/".CURRENT_ACTION;
         $n1 = str_replace("/", "", $out);
         $n2 = str_replace("/", "", $rurl);
         return ($n1 == $n2);
    }
    
    private function menu_lateral(){
        $this->LoadModel('ocorrencia/grupo', 'gr');
        $arr = $this->gr->geraMenu();
        
        $this->LoadJsPlugin('menu/treeview', 'mt');
        $this->mt->imprime();
        $var = $this->mt->draw($arr);
        $var = "<h3>Grupos de Trabalho</h3>$var";
        EventTube::addEvent('menu-lateral', $var);
    }
    
    private function addMenu($event, $arr, $obj){
        $obj->imprime();
        $var = $obj->draw($arr);
        EventTube::addEvent($event, $var);
    }
    
    private $newvars = array();
    public function setVar($name, $var){
        $this->newvars[$name] = $var;
    }
    
    public function getVars(){
        return $this->newvars;
    }
}

?>